package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBrandsByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(
                brandService.findBrandsByIds(ids)
        );
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(
                brandService.findBrandById(id)
        );
    }

    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "rows", defaultValue = "5") Long rows,
            @RequestParam(value = "key", required = false) String key) {
        return ResponseEntity.ok(
                brandService.findBrandByPage(page, rows, key)
        );
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(
                brandService.findBrandByCategoryId(id)
        );
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {
        brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}